package com.newland.starpos.partner.controller.advice;

import com.newland.starpos.partner.exception.CustomException;
import com.newland.starpos.partner.http.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * @Description: 处理请求方式错误
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseModel processMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
        log.error(e.getMessage(), e);
        return new ResponseModel(405, e.getMessage());
    }

    /**
     * @Description: 处理CustomException自定义异常
     */
    @ExceptionHandler({CustomException.class})
    @ResponseBody
    public ResponseModel processAppBizException(CustomException e) {
        return new ResponseModel(e.getCode(), e.getMessage(), e.getParams());
    }

    /**
     * @Description: 处理其他所有异常
     */
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResponseModel processOtherException(Exception e) {
        log.error(e.getMessage(), e);
        return new ResponseModel(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器异常,请稍候再试");
    }
}
